package com.example.ggpicturegg.first;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ggpicturegg.Mysql_com.ImageDao;
import com.example.ggpicturegg.Mysql_com.ImageInfo;
import  com.example.ggpicturegg.R;
import com.example.ggpicturegg.bottom.Friend_b;
import com.example.ggpicturegg.bottom.Myinfo;
import com.example.ggpicturegg.bottom.Publish_b;
import com.example.ggpicturegg.bottom.Talk_b;
import com.example.ggpicturegg.myAdapter.MyAdapter;
import com.example.ggpicturegg.tool.Loaddata;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class Care_list extends AppCompatActivity {

    private RecyclerView mRvGrid;
    private TextView tv_like;
    private TextView tv_more;
    private TextView friend;
    private TextView myinfo;
    private TextView talk;
    private ImageView im_add_x;

    private List<ImageInfo> image_list;       // 照片集合
    private ImageDao imageDao;  // image 操作实列
    private Handler mainHandler;
    private MyAdapter myAdapter;


    // 用于接受各个页面的数据
    private Bundle bundle1;
    private String sss=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_care_list);

        initView();
        transinfo();
        loadImagedb();     //加载图片数据

    }

    private void transinfo(){
        bundle1 = getIntent().getExtras();
        sss = bundle1.getString("Re_to_care");
        if(sss!=null){
            Log.d("care 接受 Re 的name", "    ---------- is ---------    "+ sss);
        } else{
            sss = bundle1.getString("like_to_care");
            Log.d("care 接受 like 的name", "    ---------- is ---------    "+ sss);
        }
    }

    private void initView(){
        im_add_x = findViewById(R.id.add_x);
        tv_like = findViewById(R.id.like);
        tv_more = findViewById(R.id.more);
        talk = findViewById(R.id.talk_x);
        friend = findViewById(R.id.friend_x);
        myinfo = findViewById(R.id.myinfo_x);
        mRvGrid = findViewById(R.id.rv_grid);
        mRvGrid.setLayoutManager(new GridLayoutManager(Care_list.this,2));
        OnClick onClick = new OnClick();
        tv_like.setOnClickListener(onClick);
        tv_more.setOnClickListener(onClick);
        talk.setOnClickListener(onClick);
        friend.setOnClickListener(onClick);
        myinfo.setOnClickListener(onClick);
        im_add_x.setOnClickListener(onClick);
        imageDao = new ImageDao();
        mainHandler = new Handler(getMainLooper()) ; //获取主线程
    }

    //加载数据
    private void loadImagedb(){
        new Thread(new Runnable() {
            @Override
            public void run() {
               // ans = imageDao.getImage_max_id();
                image_list = imageDao.getUserCareList(sss);  //获取全部用户数据

                String sss = new String(String.valueOf(image_list.size()));
                Log.d("用户个数。。。。。。。", "----------------   is    "+sss);

                for(int i=0;i<image_list.size();i++){
                    Bitmap bitmap =  Loaddata.getImage(image_list.get(i).getPicture_name(),image_list.get(i).getId());
                   // System.out.println("-----123---------- "+image_list.get(i).getPicture_name()+" -------------------------");

                    int finalI = i;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            image_list.get(finalI).setBitmap(bitmap);
                            showListViewData();
                        }
                    });
                }

            }
        }).start();
    }
    //显示数据
    private void showListViewData(){
        if(myAdapter==null){
            myAdapter = new MyAdapter(Care_list.this, image_list, new MyAdapter.OnItemClickListener() {
                @Override
                public void OnClick(int pos) {
                    Toast.makeText(Care_list.this,"click"+pos,Toast.LENGTH_SHORT).show();
                }
            },sss);
            mRvGrid.setAdapter(myAdapter);
        }
        else{
            myAdapter.setImage_list(image_list);
            myAdapter.notifyDataSetChanged();
        }
    }


    class OnClick implements View.OnClickListener{
        private  Intent intent;
        private Bundle bundle;

        @Override
        public void onClick(View v) {

            switch (v.getId()){
                case R.id.more:
                    intent = new Intent(Care_list.this,RecyclerviewActivity.class);
                    Log.d("--care 发送到 Re 的值-- ", "--  is         " +sss );
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("care_to_Re",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.like:
                    intent = new Intent(Care_list.this,Like_list.class);
                    Log.d("--care 发送到 like 的值-- ", "--  is         " +sss );
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("care_to_like",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.talk_x:
                    intent = new Intent(Care_list.this, Talk_b.class);
                    Log.d("--care 发送到 talk 的值-- ", "--  is         " +sss );
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("care_to_talk",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.friend_x:
                    intent = new Intent(Care_list.this, Friend_b.class);
                    Log.d("--care 发送到 friend 的值-- ", "--  is         " +sss );
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("care_to_friend",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.myinfo_x:
                    intent = new Intent(Care_list.this, Myinfo.class);
                    Log.d("--care 发送到 myinfo 的值-- ", "--  is         " +sss );
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("care_to_myinfo",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
                case R.id.add_x:
                    intent = new Intent(Care_list.this, Publish_b.class);
                    Log.d("--care 发送到 publish 的值--", " --  is         " +sss );
                    bundle = new Bundle();
                    //bundle.putInt("max_id",ans);
                    bundle.putString("care_to_add",sss);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    break;
            }
        }
    }

}